package com.yonyou.pmclouds.team.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.team.entity.*;
import com.yonyou.pmclouds.team.mapper.TeamEnterpriseMapper;
import com.yonyou.pmclouds.team.mapper.TeamMapper;
import com.yonyou.pmclouds.team.rmiitf.TeamEnterpriseQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = TeamEnterpriseQuery.class)
public class TeamEnterpriseQueryImpl implements TeamEnterpriseQuery {
    @Autowired
    private TeamEnterpriseMapper mapper;
    @Autowired
    private TeamMapper teamMapper;

    @Override
    public int countProjectEnterprise(String projectid) throws BusinessException {
        try {
            return mapper.countProjectEnterprise(projectid);
        } catch (Exception ex) {
            log.error("", ex);
            throw new BusinessException();
        }
    }

    @Override
    public TeamEnterpriseVO[] queryByProject(String projectid) throws BusinessException {
        if (StringUtils.isEmpty(projectid)) {
            return null;
        }
        try {
            return mapper.queryByProject(projectid);
        } catch (Exception ex) {
            log.error("", ex);
            throw new BusinessException();
        }
    }

    @Override
    public TeamEnterpriseRefVO[] queryConstructionUnitInProject(String projectid) throws BusinessException {
        if (StringUtils.isEmpty(projectid)) {
            return null;
        }
        try {
            return mapper.queryConstructionUnitInProject(projectid);
        } catch (Exception ex) {
            log.error("", ex);
            throw new BusinessException();
        }
    }

    @Override
    public TeamEnterpriseRefVO[] querySupervisionUnitInProject(String projectid) throws BusinessException {
        if (StringUtils.isEmpty(projectid)) {
            return null;
        }
        try {
            return mapper.querySupervisionUnitInProject(projectid);
        } catch (Exception ex) {
            log.error("", ex);
            throw new BusinessException();
        }
    }

    @Override
    public TeamEnterpriseRefVO[] queryConstructionUnitInProjects(String[] projectids) throws BusinessException {
        if (projectids == null || projectids.length == 0) {
            return null;
        }
        try {

            return mapper.queryConstructionUnitInProjects(projectids);
        } catch (Exception ex) {
            log.error("", ex);
            throw new BusinessException();
        }
    }

    @Override
    public TeamEnterpriseRefVO[] querySupervisionUnitInProjects(String[] projectids) throws BusinessException {
        if (projectids == null || projectids.length == 0) {
            return null;
        }
        try {
            return mapper.querySupervisionUnitInProjects(projectids);
        } catch (Exception ex) {
            log.error("", ex);
            throw new BusinessException();
        }
    }

    @Override
    public TeamEnterpriseVO[] queryByIds(String[] ids) throws BusinessException {
        if (ArrayUtils.isEmpty(ids)) {
            return null;
        }
        try {
            return mapper.queryByIds(ids);
        } catch (Exception ex) {
            log.error("", ex);
            throw new BusinessException();
        }
    }

    @Override
    public TeamEnterpriseViewVO[] queryEnterpriseViewVOsByProject(String projectid) throws BusinessException {
        TeamEnterpriseVO[] teamEnterpriseVOS = queryByProject(projectid);
        TeamMemberVO[] teamMemberVOS = teamMapper.queryTeamByProject(projectid);
        Map<String,List<TeamMemberViewVO>> pkEnterAndMemberMap = getPkEnterAndMemberViewMap(teamMemberVOS);
        TeamEnterpriseViewVO[] teamEnterpriseViewVOS = getEnterpriseViewVOs(teamEnterpriseVOS, pkEnterAndMemberMap);

        return teamEnterpriseViewVOS;
    }

    @Override
    public TeamEnterpriseVO[] queryByProjects(String[] pkProjects) throws BusinessException {
        return mapper.queryByProjects(pkProjects);
    }

    @Override
    public TeamEnterpriseVO[] queryByPkEnts(String[] pkEnts) throws BusinessException {
        return mapper.queryByPkEnts(pkEnts);
    }

    @Override
    public List<TeamEnterpriseVO> queryConstructEnterByProject(String pkProject, String pkTenant) throws BusinessException {
        List<TeamEnterpriseVO> teamEnterpriseVOs = mapper.queryConstructEnterByProject(pkProject, pkTenant);
        // 为空则去掉企业角色过滤条件
        if (teamEnterpriseVOs == null || teamEnterpriseVOs.size() == 0) {
            List<TeamEnterpriseVO> results = mapper.queryByProjectAndTenant(pkProject, pkTenant);
            // 为空则去团队成员表查找企业
            if (results == null || results.size() == 0) {
                List<TeamMemberVO> teamMemberVOs = teamMapper.queryEnterpriseByProjectAndTenant(pkTenant, pkProject);
                if (teamMemberVOs != null && teamMemberVOs.size() > 0) {
                    List<TeamEnterpriseVO> enterpriseVOList = new ArrayList<>();
                    TeamMemberVO teamMemberVO = teamMemberVOs.get(0);
                    TeamEnterpriseVO currVO = new TeamEnterpriseVO();
                    if (teamMemberVO != null) {
                        currVO.setPkEnterprise(teamMemberVO.getPkEnterprise());
                    }
                    enterpriseVOList.add(currVO);
                    return enterpriseVOList;
                }
            }
            return results;
        }
        return teamEnterpriseVOs;
    }

    /**
     * 获取企业ID与TeamMemberViewVO的对应关系
     *
     * @param teamMemberVOs
     * @return
     */
    private Map<String,List<TeamMemberViewVO>> getPkEnterAndMemberViewMap(TeamMemberVO[] teamMemberVOs) {
        Map<String,List<TeamMemberViewVO>> pkEnterAndMemberMap = new HashMap<String,List<TeamMemberViewVO>>();

        if(ArrayUtils.isEmpty(teamMemberVOs))
            return pkEnterAndMemberMap;

        String tempPkEnter ;

        for (int i = 0; i < teamMemberVOs.length;i++) {
            TeamMemberViewVO teamMemberViewVO = new TeamMemberViewVO();
            for (String field :
                    TeamMemberViewVO.transFields) {
                BeanHelper.setProperty(teamMemberViewVO,field,BeanHelper.getProperty(teamMemberVOs[i],field));
            }

            tempPkEnter = teamMemberViewVO.getPkEnterprise();

            if(pkEnterAndMemberMap.get(tempPkEnter) == null){
                List<TeamMemberViewVO> teamMemberViewVOList = new ArrayList<TeamMemberViewVO>();
                teamMemberViewVOList.add(teamMemberViewVO);
                pkEnterAndMemberMap.put(tempPkEnter,teamMemberViewVOList);
            }else{
                pkEnterAndMemberMap.get(tempPkEnter).add(teamMemberViewVO);
            }
        }
        return pkEnterAndMemberMap;
    }

    /**
     * 组装参与企业与参与人员的对应关系
     *
     * @param teamEnterpriseVOs
     * @param pkEnterAndMemberMap
     * @return
     */
    private TeamEnterpriseViewVO[] getEnterpriseViewVOs(TeamEnterpriseVO[] teamEnterpriseVOs, Map<String, List<TeamMemberViewVO>> pkEnterAndMemberMap) {
        TeamEnterpriseViewVO[] teamEnterpriseViewVOs = new TeamEnterpriseViewVO[teamEnterpriseVOs.length];

        for (int i = 0; i < teamEnterpriseVOs.length;i++) {
            TeamEnterpriseViewVO teamEnterpriseViewVO = new TeamEnterpriseViewVO();
            for (String field :
                    TeamEnterpriseViewVO.transFields) {
                BeanHelper.setProperty(teamEnterpriseViewVO,field,BeanHelper.getProperty(teamEnterpriseVOs[i],field));
            }

            List<TeamMemberViewVO> memberViewVOList =  pkEnterAndMemberMap.get(teamEnterpriseViewVO.getPkEnterprise());

            if(memberViewVOList != null && memberViewVOList.size() > 0){
                teamEnterpriseViewVO.setTeamMemberViewVOs(memberViewVOList.toArray(new TeamMemberViewVO[memberViewVOList.size()]));
            }

            teamEnterpriseViewVOs[i] = teamEnterpriseViewVO;
        }
        return teamEnterpriseViewVOs;
    }
}
